package gases.as3.librarys.components.ui.special
{
	import gases.as3.librarys.components.swf.StyleConfig;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.control.GButton;
	import gases.as3.librarys.components.ui.control.GLabelButton;
	import gases.as3.librarys.components.ui.control.group.GButtonGroup;
	
	public class GStateButtonGroup extends GUIComponents
	{
		public static const BUTTON_GAP:Number = 10;
		
		public function GStateButtonGroup()
		{
			super();
		}
		
		override protected function initialization():void
		{
			_dataArray = new Array();
			_btnsArray = new Array();
			_roleInfoArray = new Array();
			_buttonGroup = new GButtonGroup();
			
			this.addChild(_buttonGroup);
			
			for(var i:int=0;i<5;i++)
			{
				var button:GLabelButton = new GLabelButton("创建新角色",0xFFFFFF,140,40,16);
				button.id = "button"+(i+1);
				button.skin = StyleConfig.instance.retSkin("default_button");
				button.y = i*(BUTTON_GAP+button.Gheight);
				_buttonGroup.addButton(button);
				_btnsArray[i] = button;
			}
		}
		
		/**
		 *相应格式的数据 
		 * @param value
		 * 
		 */		
		public function setButtonsData(value:Array):void
		{
			_dataArray  = value;
			fillData();
		}
		
		
		private function fillData():void
		{
			if(_dataArray.length < 1)
			{
				return;
			}
			for(var k:int=0;k<_roleInfoArray.length;k++)
			{
				var dis:ChooseRoleInfoComponent = _roleInfoArray[k] as ChooseRoleInfoComponent;
				if(this.contains(dis))
				{
					this.removeChild(dis);
				}
			}
			for(var i:int=0;i<_dataArray.length;i++)
			{
				var obj:Object = _dataArray[i];
				var button:GLabelButton = _buttonGroup.selectButtonById(("button"+(i+1))) as GLabelButton;
				if(button)
				{
					button.text = "";
					var roleInfo:ChooseRoleInfoComponent = new ChooseRoleInfoComponent();
					if(obj.image)
					{
						roleInfo.drawImage(obj.image);
					}
					if(obj.name)
					{
						roleInfo.setRoleName(obj.name);
					}
					if(obj.level)
					{
						roleInfo.setRoleLevel(obj.level);
					}
					if(obj.job)
					{
						roleInfo.setRoleJob(obj.job);
					}
					this.addChild(roleInfo);
					_roleInfoArray[i] = roleInfo;
					roleInfo.x = button.x + (button.Gwidth - roleInfo.Gwidth)/2;
					roleInfo.y =  button.y + (button.Gheight - roleInfo.Gheight)/2;
				}
			}
			if(_dataArray.length >= 5)
			{
				return;
			}
			for(var j:int=5;j>_dataArray.length;j--)
			{
				var btn:GLabelButton = _buttonGroup.selectButtonById(("button"+j)) as GLabelButton;
				if(btn)
				{
					btn.text = "创建新角色";
				}
			}
		}
		
		public function get dataArray():Array
		{
			return _dataArray;
		}
		
		override public function dispose():void
		{
			super.dispose();
		}
		
		private var _buttonGroup:GButtonGroup;
		
		private var _dataArray:Array;
		private var _btnsArray:Array;
		private var _roleInfoArray:Array;
	}
}